package com.group01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.group01.enums.CodeAndMessageEnum;
import com.group01.mapper.CommodityMapper;
import com.group01.pojo.Collect;
import com.group01.pojo.Commodity;
import com.group01.result.Result;
import com.group01.service.CollectService;
import com.group01.mapper.CollectMapper;
import com.group01.utils.BeanCopyUtils;
import com.group01.utils.CommonUtils;
import com.group01.vo.Collect.CollectVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 19559
* @description 针对表【es_collect(收藏表)】的数据库操作Service实现
* @createDate 2024-06-23 07:55:02
*/
@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService{

    /**
     * 注入
     */
    private final CollectMapper collectMapper;
    private final CommodityMapper commodityMapper;

    public CollectServiceImpl(CollectMapper collectMapper, CommodityMapper commodityMapper) {
        this.collectMapper = collectMapper;
        this.commodityMapper = commodityMapper;
    }

    /**
     * 获取用户收藏信息
     * @return {@link Result }<{@link List }<{@link CollectVO }>>
     */
    @Override
    public Result<List<CollectVO>> getCollectList() {
        // 获取用户id
        Integer userId = CommonUtils.getLocalId();
        MPJLambdaWrapper<Collect> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAs(Collect::getId, Collect::getId)
                .selectAs(Commodity::getName , Collect::getName)
                .selectAs(Commodity::getCover , Collect::getCover)
                .selectAs(Commodity::getPrice , Collect::getPrice)
                .selectAs(Collect::getGoodsId , Collect::getBookId)
                .selectAs(Commodity::getDescription, Collect::getDescription)
                .leftJoin(Commodity.class, Commodity::getId, Collect::getGoodsId)
                .eq(Collect::getUserId, userId);

        List<Collect> collects = collectMapper.selectList(wrapper);

        // Bean拷贝
        return Result.success(CodeAndMessageEnum.FIND_RESULT, BeanCopyUtils.copyBeanList(collects, CollectVO.class));
    }

    /**
     * 删除与批量删除图书收藏
     * @param ids 商品收藏id集合
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> deleteCollects(List<Integer> ids) {
        collectMapper.deleteBatchIds(ids);
        return Result.success(CodeAndMessageEnum.DELETE_SUCCESS);
    }

    /**
     * 添加收藏
     * @param id 商品id
     * @return {@link Result }<{@link String }>
     */
    @Override
    public Result<String> addCollect(Integer id) {
        Long count = commodityMapper.selectCount(new LambdaQueryWrapper<Commodity>().eq(Commodity::getId, id));
        if(count == 0) {
            return Result.error(CodeAndMessageEnum.FIND_COMMODITY_ID_FAIL);
        }
        // 获取用户id
        Integer userId = CommonUtils.getLocalId();
        count = collectMapper.selectCount(new LambdaQueryWrapper<Collect>().eq(Collect::getUserId, userId).eq(Collect::getGoodsId, id));
        if(count > 0) {
            return Result.error(CodeAndMessageEnum.COLLECT_EXIST);
        }
        Collect collect = new Collect().setUserId(userId).setGoodsId(id);

        collectMapper.insert(collect);

        return Result.success(CodeAndMessageEnum.SUCCESS_COLLECT);
    }

}




